package com.lvmama.pangolin.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.EarlyWarning;
import com.lvmama.pangolin.entity.WarningConfig;
import com.lvmama.pangolin.mybatis.mapper.EarlyWarningMapper;
import com.lvmama.pangolin.service.EarlyWarningService;
@Transactional
@Service
public class EarlyWarningServiceImpl implements EarlyWarningService{
	
	@Autowired
	private EarlyWarningMapper earlyWarningMapper;
	
	private Logger logger = Logger.getLogger(EarlyWarningService.class);

	@Override
	public int insertEarlyWarning(EarlyWarning warning) throws Exception
	{
		return earlyWarningMapper.insertEarlyWarning(warning);
	}

	@Override
	public List<EarlyWarning> queryEarlyWarning(EarlyWarning warning) throws Exception
	{
		return earlyWarningMapper.queryEarlyWarning(warning);
	}

	@Override
	public List<EarlyWarning> queryWarningLogBySql(Map<String,Object> param) throws Exception
	{
		return earlyWarningMapper.queryWarningLogBySql(param);
	}

	@Override
	public int updateEarlyWarningStatus(EarlyWarning warning)
	{
		return earlyWarningMapper.updateEarlyWarningStatus(warning);
	}

	@Override
	public void DealEarlyWarning(WarningConfig wconfig,String content)
	{
		logger.info("处理告警信息开始...");
		if (!checkSended(wconfig))
		{
			try
			{
				if (StringTools.isValid(content))
				{
					// 记录告警日志
					insertEarlyWarn(wconfig,content);
				}
				
			}
			catch (Exception e)
			{
				logger.error("预警信息异常!", e);
			}
		}
		logger.info("处理告警信息结束...");
	}
	
	private void insertEarlyWarn(WarningConfig wconfig,String content) throws Exception
	{
		EarlyWarning earlyWarning = new EarlyWarning();
		earlyWarning.setContact_info("");
		earlyWarning.setInsert_date(new Date());
		earlyWarning.setStatus("0");
		earlyWarning.setWarning_level(wconfig.getWarn_level());
		earlyWarning.setWarn_code(wconfig.getWarn_code());
		earlyWarning.setWarning_mode(wconfig.getWarn_mode());
		earlyWarning.setWarning_content(content);
		earlyWarning.setTimer_code("0000");
		earlyWarning.setBusi_type("TICKET");
		earlyWarning.setException_type("C");
		earlyWarning.setWarning_mode("wchat");
		Date date =  new Date();
		earlyWarning.setWarning_time(new Timestamp(date.getTime()));
		insertEarlyWarning(earlyWarning);
	}
	
	/**
	 * 
	* @Title: checkSend 
	* @Description: TODO判断告警是否已发送默认company_code,log_type,busi_type,task_id相同作为同一告警
	* @param @param wconfig
	* @param @param earlyWarning
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	private boolean checkSended(WarningConfig wconfig)
	{
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("sql", wconfig.getWarn_sql());
		param.put("warn_code", wconfig.getWarn_code());
		param.put("warning_period", wconfig.getWarn_period());
		boolean flag = false;
		try
		{
			List<EarlyWarning> warnList = queryWarningLogBySql(param);
			if(warnList != null && !warnList.isEmpty())
			{
				flag = true;
				logger.info("周期内该预警已经发送！");
			}
		}catch (Exception e)
		{
			logger.error("查询预警记录异常！",e);
		}
		
		return flag;
		
	}
	
	
	
	
}
